﻿using System;
using System.Net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using ProxyFinder.Strategies;
using SoftwareApproach.TestingExtensions;
using CredentialManagement;

namespace ProxyFinder.Tests {
    [TestClass]
    public class SavedCredentialStrategyTests {

        [TestMethod]
        public void SavedCredentialStrategy_Create() {
            new SavedCredentialStrategy(new DefaultCredentialProvider()).ShouldNotBeNull();
        }

        [TestMethod]
        public void SavedCredentialStrategy_ShouldThrowException_WhenCredentialProvider_IsNull() {
            Testing.ShouldThrowException<ArgumentNullException>(() => new SavedCredentialStrategy(null));
        }

        [TestMethod]
        public void SavedCredentialStrategy_ShouldReturnNull_WhenNoSavedCredentials() {
            var credProvider = new Mock<ICredentialProvider>();
            credProvider.Setup(p => p.GetCredentials(It.IsAny<string>())).Returns(new NetworkCredential[]{});
            SavedCredentialStrategy strategy = new SavedCredentialStrategy(credProvider.Object);
            strategy.GetProxy(new Uri("http://www.google.com")).ShouldBeNull();
        }

        [TestMethod]
        public void SavedCredentialStrategy_ShouldReturnCredential_WhenSavedCredentialsExist() {
            var credProvider = new Mock<ICredentialProvider>();
            credProvider.Setup(p => p.GetCredentials(It.IsAny<string>())).Returns(new[] { 
                new NetworkCredential { 
                    UserName ="username",
                    Password = "password" 
                }
            });

            SavedCredentialStrategy strategy = new SavedCredentialStrategy(credProvider.Object);
            strategy.GetProxy(new Uri("http://www.google.com")).ShouldNotBeNull();
        }
    }
}
